import {
  Body,
  Controller,
  Get,
  Post,
  Put,
  Param,
  Request,
  HttpException,
  HttpStatus,
  Query
} from '@nestjs/common'

import {
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiTags,
  ApiBody,
  ApiParam,
  ApiQuery,
  ApiHeader
} from '@nestjs/swagger'

import { UserService } from './user.service'
import {
  UserResponseDto,
  ResponseDto,
  BaseErrorDto,
  UpdateUserDto
} from './dtos/Response.dto'
import { QueryPage } from '../apiProperty'
import { IsPublic } from '../../common/decorator/is-public/is-public.decorator'

import { UserEntity } from '../../entitys'

import { CheckId } from '../../common/decorator/check-id/check-id.decorator'

const ParamsError = '参数错误'

@ApiTags('用户相关')
@ApiBearerAuth()
@Controller('user')
export class UserController {
  constructor(private userService: UserService) {}

  @ApiResponse({ status: 200, type: UserResponseDto })
  @ApiResponse({ status: 400, type: BaseErrorDto })
  @ApiOperation({ summary: '获取用户信息' })
  @Get('/:id/get')
  async getUserInfoById(@Param('id') id: string, @CheckId() check: boolean) {
    if (check) {
      return await this.userService.getUserInfoById(id)
    }
    throw new HttpException(ParamsError, HttpStatus.BAD_REQUEST)
  }

  @ApiOperation({ summary: '修改用户信息' })
  @ApiBody({ type: UpdateUserDto })
  @ApiResponse({ status: 200, type: UserResponseDto })
  @ApiResponse({ status: 400, type: BaseErrorDto })
  @Put('/:id/update')
  async updateUserInfoById(
    @Param('id') id: string,
    @Body() body: UpdateUserDto,
    @CheckId() check: boolean
  ) {
    if (check) {
      return await this.userService.updateUserInfo(id, body)
    }
    throw new HttpException(ParamsError, HttpStatus.BAD_REQUEST)
  }

  @ApiOperation({ summary: '关注用户列表' })
  @ApiResponse({ status: 200, type: ResponseDto })
  @ApiResponse({ status: 400, type: BaseErrorDto })
  @Get('/:id/follows')
  async getUserFollows(
    @Param('id') id: string,
    @Query() query: QueryPage,
    @CheckId() check: boolean
  ) {
    if (check) {
      return await this.userService.userFollowsInfo(id, query)
    }
    throw new HttpException(ParamsError, HttpStatus.BAD_REQUEST)
  }

  @ApiOperation({ summary: '关注用户' })
  @ApiResponse({ status: 200, type: ResponseDto })
  @ApiResponse({ status: 400, type: BaseErrorDto })
  @Get('/:id/follow')
  async followsUser(
    @Param('id') id: string,
    @Request() req: Record<string | symbol, Record<string | symbol, string>>
  ) {
    return await this.userService.followsUser(req.user.id, id)
  }

  @ApiOperation({ summary: '取消关注用户' })
  @ApiResponse({ status: 200, type: ResponseDto })
  @ApiResponse({ status: 400, type: BaseErrorDto })
  @Get('/:id/unfollow')
  async unfollowUser(
    @Param('id') id: string,
    @Request() req: Record<string | symbol, Record<string | symbol, string>>
  ) {
    return await this.userService.unfollowUser(req.user.id, id)
  }

  @ApiOperation({ summary: '用户评论列表' })
  @ApiResponse({ status: 200, type: ResponseDto })
  @ApiResponse({ status: 400, type: BaseErrorDto })
  @Get('/:id/comments')
  async userComments(
    @Param('id') id: string,
    @Query() query: QueryPage,
    @CheckId() check: boolean
  ) {
    if (check) {
      return await this.userService.userComments(id, query)
    }
    throw new HttpException(ParamsError, HttpStatus.BAD_REQUEST)
  }

  @ApiOperation({ summary: '收藏文章列表' })
  @ApiResponse({ status: 200, type: ResponseDto })
  @ApiResponse({ status: 400, type: BaseErrorDto })
  @Get('/:id/star')
  async userStar(
    @Param('id') id: string,
    @Query() query: QueryPage,
    @CheckId() check: boolean
  ) {
    if (check) {
      return await this.userService.userStar(id, query)
    }
    throw new HttpException(ParamsError, HttpStatus.BAD_REQUEST)
  }
}

@ApiTags('用户相关')
@Controller('user')
export class UserControllerTow {
  constructor(private userService: UserService) {}

  @IsPublic()
  @ApiOperation({ summary: '获取用户信息' })
  @ApiResponse({ status: 200, type: ResponseDto })
  @ApiResponse({ status: 400, type: BaseErrorDto })
  @Get('/:id/get/tow')
  async getUserInfoById(@Param('id') id: string) {
    return await {
      data: {
        code: 1,
        id
      }
    }
  }
}
